package com.hotel.suqiang.dingdonegurad;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hotel.suqiang.dingdonegurad.bean.HomeGridViewItemBean;
import com.hotel.suqiang.dingdonegurad.utils.Contants;
import com.hotel.suqiang.dingdonegurad.utils.SharedPreferencesUtis;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private ImageView iv_home_logo;
    private ImageView iv_home_setting;
    private GridView gv_home_functions;



    private final static String[] TITLES = new String[] { "手机防盗", "骚扰拦截",
            "软件管家", "进程管理", "流量统计", "手机杀毒", "缓存清理", "常用工具" };
    private final static String[] DESCS = new String[] { "远程定位手机", "全面拦截骚扰",
            "管理您的软件", "管理运行进程", "流量一目了然", "病毒无处藏身", "系统快如火箭", "工具大全" };

    private final static int[] ICONS = new int[] { R.drawable.sjfd,
            R.drawable.srlj, R.drawable.rjgj, R.drawable.jcgl, R.drawable.lltj,
            R.drawable.sjsd, R.drawable.hcql, R.drawable.cygj };


    protected List<HomeGridViewItemBean> items = null;
    private EditText et_password_first;
    private EditText et_password_second;
    private Button bt_password_ok;
    private Button bt_password_cancle;
    private AlertDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        initView();
    }

    private void initView() {

        iv_home_logo = (ImageView)findViewById(R.id.iv_home_logo);
        iv_home_setting = (ImageView)findViewById(R.id.iv_home_setting);
        gv_home_functions = (GridView)findViewById(R.id.gv_home_functions);

        setupAnimation();


        items = new ArrayList<HomeGridViewItemBean>();
        HomeGridViewItemBean bean = null;
        for (int i = 0; i < ICONS.length; i++){
            bean = new HomeGridViewItemBean();
            bean.iconId = ICONS[i];
            bean.desc = DESCS[i];
            bean.title = TITLES[i];
            items.add(bean);
        }

        items.addAll(items);


        gv_home_functions.setAdapter(new MyAdapter());
        gv_home_functions.setOnItemClickListener(this);

        iv_home_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(HomeActivity.this, SettingActivity.class);

                startActivity(intent);

                Toast.makeText(getApplicationContext(), "设置点击", Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        HomeGridViewItemBean bean = items.get(position);

        Toast.makeText(getApplicationContext(), "点击" + bean.title , Toast.LENGTH_SHORT).show();

        switch (position){
            case 0:{
                showPasswordDialog();
                break;
            }
            case 1:{
                break;
            }
            case 2:{
                break;
            }
            case 3:{
                break;
            }
            case 4:{
                break;
            }
            case 5:{
                break;
            }
            case 6:{
                break;
            }
            case 7:{
                break;
            }
            case 8:{
                break;
            }
            default:{

            }
        }
    }

    private void showPasswordDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        View dialogView = View.inflate(this, R.layout.set_pastword_layout,null);


        et_password_first = (EditText) dialogView.findViewById(R.id.et_password_first);

        et_password_second = (EditText) dialogView.findViewById(R.id.et_password_second);


        bt_password_ok = (Button) dialogView.findViewById(R.id.bt_password_ok);

        bt_password_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String firstPassword = et_password_first.getText().toString();

                String secondPassword = et_password_second.getText().toString();

                if (firstPassword == null || firstPassword.isEmpty()){
                    Toast.makeText(getApplicationContext(), "密码不能为空", Toast.LENGTH_LONG);
                    return;
                }

                if (!firstPassword.equals(secondPassword)){
                    Toast.makeText(getApplicationContext(), "两次密码不同", Toast.LENGTH_LONG);
                    return;
                }

                SharedPreferencesUtis.saveString(HomeActivity.this, Contants.PASS_WORD_KEY, firstPassword);

                dialog.dismiss();

                Toast.makeText(getApplicationContext(), "密码设置成功", Toast.LENGTH_LONG);
            }
        });

        bt_password_cancle = (Button) dialogView.findViewById(R.id.bt_password_cancle);

        bt_password_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });


        builder.setView(dialogView);

        dialog = builder.create();

        dialog.show();


    }


    private  class MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int position) {
            return items.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view = View.inflate(getApplicationContext(), R.layout.activity_home_gridview_item, null);

            HomeGridViewItemBean bean = items.get(position);

            ImageView icon = (ImageView) view.findViewById(R.id.home_grid_item_icon);

            TextView title = (TextView) view.findViewById(R.id.home_grid_item_title);

            TextView desc = (TextView) view.findViewById(R.id.home_grid_item_desc);

            icon.setImageResource(bean.iconId);
            title.setText(bean.title);
            desc.setText(bean.desc);

            return view;
        }


    }



    private void setupAnimation() {

        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(iv_home_logo, "RotationY", 0f, 90f, 270f, 360f);

        objectAnimator.setDuration(2000);

        objectAnimator.setRepeatCount(ObjectAnimator.INFINITE);

        objectAnimator.setRepeatMode(ObjectAnimator.RESTART);

        objectAnimator.start();
    }
}
